package com.iamback.dy.examine.controller.admin.examine;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.UUID; 

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iamback.dy.entity.ContentExuser;
import com.iamback.dy.entity.ExContentView;
import com.iamback.dy.entity.User;
import com.iamback.dy.examine.common.JsonResult;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.controller.BaseController;
import com.iamback.dy.examine.controller.modle.Payload;
import com.iamback.dy.examine.controller.webSocket.WebSocketServer;
import com.iamback.dy.examine.service.IContentExuserService;
import com.iamback.dy.examine.service.IContentUnreviewedService;
import com.iamback.dy.examine.service.IRoleService;

@Controller
@RequestMapping("/examine/content")
public class ExamineContentController extends BaseController  {
	
	@Autowired
	private IContentUnreviewedService  contentUnreviewedService; 
	
	@Autowired
	private IContentExuserService contentExuserService;
	@Autowired
	private IRoleService roleService;

	@Autowired
	private WebSocketServer socketServer;
	
	@RequestMapping(value ={"/index"})
	public String  examineImg(){
		return "examine/index";
	}
	/**
	 * 审核列表
	 * @return
	 */
	@RequestMapping(value = { "/list" })
	@ResponseBody
	public Page<ExContentView>  getExuserContentByList(Integer pageNumber,Integer pageSize){
		User userId =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		if(userId!=null){ 
//			SimpleSpecificationBuilder<ContentExuser> builder = new SimpleSpecificationBuilder<ContentExuser>();
//			builder.add("exuserId", Operator.eq.name(), String.valueOf(userId.getId()));
//			builder.add("exResult", Operator.eq.name(), "0");  
//			int pageNum = Integer.parseInt(request.getParameter("pageNumber")) - 1;
//			int sizeNum = Integer.parseInt(request.getParameter("pageSize"));
			Page<ExContentView> page  =contentExuserService.findContentExuserByUserId(userId.getId(),pageNumber,pageSize);
			//查看角色是否需要分配任务
			if(page.getTotalElements()<20){
				contentExuserService.distributionBy(String.valueOf(userId.getId()),null,page.getTotalElements());
			}
		return page;
		}
		return null;
	} 
	 
	
	/**
	 * 审核  
	 * @return
	 */
	@RequestMapping(value = { "/update" } , method = RequestMethod.POST)
	@ResponseBody
	public JsonResult updateExuserContent(@RequestBody List<Payload> payloads){
		try{
			User userId =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
			if(userId==null){
				throw new Exception();
			}
			if(userId!=null&&userId.getId()!=null){ 
	           if(!payloads.isEmpty()){
					String  uuid = UUID.randomUUID().toString(); 
					for(Payload  payload :payloads){
						ContentExuser exuserContent= new ContentExuser();
						exuserContent.setExResult(payload.getExResult());
						exuserContent.setExTime(new Date());
						exuserContent.setOperationCode(uuid);
						exuserContent.setId(Long.valueOf(payload.getId()));
						exuserContent.setExuserId(Long.valueOf(userId.getId()));
						Long num = contentExuserService.upateExConten(exuserContent);
						if(num!=0){
							WebSocketServer.sendMessage(String.valueOf(num), userId.getUserName());
						}
					}
	           }
			}
		} catch (Exception e) {
			return JsonResult.failure(e.getMessage());
		}
		return JsonResult.success();
	}
	@RequestMapping(value = { "/getexnum" })
	@ResponseBody
	public JsonResult getExNum(){
		User userId =(User) SecurityUtils.getSubject().getSession().getAttribute("loginUserInfo");
		Long num =contentExuserService.getExtaskNum(Long.valueOf(userId.getId()));
		return JsonResult.success("成功", num);
	}
}
